<template>
  <div class="login-page">
    <el-card class="el-card">
   <template #header>黑马面经运营后台</template>
  <!-- 表单校验3要素：
  1.给 el-form 注册model属性
  2.给 el-form 注册rules属性
  2.给 el-form-item 添加prop属性
   -->
   <el-form  :model="form" :rules="rules" ref="loginForm">
  <el-form-item prop="username" label="用户名">
    <el-input placeholder='请输入用户名' v-model="form.username"></el-input>
  </el-form-item>
   <el-form-item prop="password" label="密码">
    <el-input placeholder='请输入密码' v-model="form.password"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="hlogin">登录</el-button>
    <el-button @click="hreset">重置</el-button>
  </el-form-item>
  </el-form>
</el-card>

  </div>
</template>

<script>
import { login } from '@/api/user'
export default {
  name: 'HmH5PcIndex',

  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在6-15个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          // *表示
          { pattern: /^\w{5,10}$/, message: '长度在5-10个字符', trigger: 'blur' }
        ]
      }

    }
  },

  methods: {
    hlogin () {
      this.$refs.loginForm.validate(async valid => {
        // 这个函数汇总表单验证通过后自动执行
        console.log(valid)
        if (!valid) return
        // console.log('发送请求')
        const res = await login(this.form)
        // console.log(res)
        this.$store.commit('user/getToken', res.data.token)
        this.$router.push('/')
        this.$message.success('登录成功')
      })
    },
    hreset () {
      this.$refs.loginForm.resetFields()
    }

  }
}
</script>

<style lang="scss" scoped>
.login-page {
   min-height: 100vh;
  background: url(@/assets/login-bg.svg) no-repeat center / cover;
  display: flex;
  align-items: center;
  justify-content: space-around;
    .el-card {
  width: 420px;
  margin: auto;
 ::v-deep .el-card__header{
  background-color: rgba(114,124,245,1);
  text-align: center;
  color: #fff;

  }
}
  }

</style>
